//
//  jniutil.m
//  CorP5e
//
//  Created by Matthew Giant on 9/27/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "jniutil.h"

NSString *JavaStringToNSString(JNIEnv *env, jstring aString)
{
	if(aString == NULL)
		return nil;
	
	const jchar *chars = (*env)->GetStringChars(env, aString, NULL);
	NSString *resultString = [NSString stringWithCharacters:(UniChar *)chars length:(*env)->GetStringLength(env, aString)];
	(*env)->ReleaseStringChars(env, aString, chars);
	return resultString;
}

jstring NSStringToJavaString(JNIEnv *env, NSString *aString)
{
	if(aString == nil)
		return NULL;
	
	jsize buflength = [aString length];
	unichar buffer[buflength];
	[aString getCharacters:buffer];
	return (*env)->NewString(env, (jchar *)buffer, buflength);
}


NSString *jintToFilterConstant( int intVal )
{
	switch ( intVal )
	{
		case 0:
			return kCICategoryDistortionEffect;
		case 1:
			return kCICategoryGeometryAdjustment;
		case 2:
			return kCICategoryCompositeOperation;
		case 3:
			return kCICategoryHalftoneEffect;
		case 4:
			return kCICategoryColorAdjustment;
		case 5:
			return kCICategoryColorEffect;
		case 6:
			return kCICategoryTransition;
		case 7:
			return kCICategoryTileEffect;
		case 8:
			return kCICategoryGenerator;
		case 9:
			return kCICategoryReduction;
		case 10:
			return kCICategoryGradient;
		case 11:
			return kCICategoryStylize;
		case 12:
			return kCICategorySharpen;
		case 13:
			return kCICategoryBlur;
		case 14:
			return kCICategoryVideo;
		case 15:
			return kCICategoryStillImage;
		case 16:
			return kCICategoryInterlaced;
		case 17:
			return kCICategoryNonSquarePixels;
		case 18:
			return kCICategoryHighDynamicRange ;
		case 19:
			return kCICategoryBuiltIn;
		case 20:
			return kCICategoryFilterGenerator;
		case 30:
			return kCIOutputImageKey;
		case 31:
			return kCIInputBackgroundImageKey;
		case 32:
			return kCIInputImageKey;
		case 33:
			return kCIInputTimeKey;
		case 34:
			return kCIInputTransformKey;
		case 35:
			return kCIInputScaleKey;
		case 36:
			return kCIInputAspectRatioKey;
		case 37:
			return kCIInputCenterKey;
		case 38:
			return kCIInputRadiusKey;
		case 39:
			return kCIInputAngleKey;
		case 40:
			return kCIInputRefractionKey;
		case 41:
			return kCIInputWidthKey;
		case 42:
			return kCIInputSharpnessKey;
		case 43:
			return kCIInputIntensityKey;
		case 44:
			return kCIInputEVKey;
		case 45:
			return kCIInputSaturationKey;
		case 46:
			return kCIInputColorKey;
		case 47:
			return kCIInputBrightnessKey;
		case 48:
			return kCIInputContrastKey;
		case 49:
			return kCIInputGradientImageKey;
		case 50:
			return kCIInputMaskImageKey;
		case 51:
			return kCIInputShadingImageKey;
		case 52:
			return kCIInputTargetImageKey;
		case 53:
			return kCIInputExtentKey;
		default:
			return nil;
	}
}

void dumpImage( CIImage *img )
{
	
	NSBitmapImageRep* rep = [[[NSBitmapImageRep alloc] initWithCIImage:img] autorelease];
	
	CGImageRef cgImage = rep.CGImage;
	
	CFURLRef fileUrl = CFURLCreateWithFileSystemPath ( NULL,
													  CFSTR( "/Users/giantm/testimage.png" ),
													  kCFURLPOSIXPathStyle,
													  false );
	
	CGImageDestinationRef dest = CGImageDestinationCreateWithURL(	fileUrl,
																 kUTTypePNG,
																 1,
																 NULL );
	
	CGImageDestinationAddImage(	dest, 
							   cgImage, 
							   NULL );
	
	CGImageDestinationFinalize( dest );
	
	CFRelease( dest );
	CFRelease( fileUrl );
	[rep release];
}

